package com.bw.ods;

import com.bw.MyKafkaUtil;
import com.bw.gmall.realtime.common.constant.Constant;
import com.ververica.cdc.connectors.mysql.source.MySqlSource;
import com.ververica.cdc.connectors.mysql.table.StartupOptions;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.base.DeliveryGuarantee;
import org.apache.flink.connector.kafka.sink.KafkaRecordSerializationSchema;
import org.apache.flink.connector.kafka.sink.KafkaSink;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Properties;

// ODS
public class FlinkCDC {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // 配置项
        Properties props = new Properties();
        // SSL关闭
        props.setProperty("useSSL", "false");
        props.setProperty("allowPublicKeyRetrieval", "true");
        MySqlSource<String> mySqlSource = MySqlSource.<String>builder()
                .hostname("hadoop102")
                .port(3306)
                .username("root")
                .password("123456")
                .databaseList("gmall")
                .tableList("gmall.base_trademark")
                .jdbcProperties(props)
                .deserializer(new CustomDebezium())
                .startupOptions(StartupOptions.initial())
                .build();

        // 写入到Kafka
        // 把source加入到env里面取
        DataStreamSource<String> dataStreamSource = env.fromSource(mySqlSource, WatermarkStrategy.noWatermarks(),"mysql_source");
        //3.打印数据
        dataStreamSource.print();
//        //4.存入kafka
//        String sinkTopic = "ods_base_db";
////        dataStreamSource.addSink(MyKafkaUtil.getKafkaProducer(sinkTopic));
//        dataStreamSource.sinkTo(KafkaSink.<String>builder()
//                .setBootstrapServers(Constant.KAFKA_BROKERS)
//                .setRecordSerializer(KafkaRecordSerializationSchema.builder()
//                        .setTopic(sinkTopic)
//                        .setValueSerializationSchema(new SimpleStringSchema())
//                        .build()
//                )
//                .setDeliveryGuarantee(DeliveryGuarantee.EXACTLY_ONCE)
//                .setTransactionalIdPrefix("bw-" + sinkTopic + System.currentTimeMillis())
//                .setProperty("transaction.timeout.ms", 15 * 60 * 1000 + "")
//                .build());

//        //5.启动
        env.execute("FlinkCDC");
    }

}
